package com.lanou.dllo.findfood.fragment;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;

import com.lanou.dllo.findfood.activity.MenuDescipetinActivity;
import com.lanou.dllo.findfood.adpter.MenuReclyclerAdpter;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.database.MenuHealDataBaseUtli;
import com.lanou.dllo.findfood.dialog.CookProgressDialog;
import com.lanou.dllo.findfood.entity.Menu;
import com.lanou.dllo.findfood.entity.MenuResult;
import com.lanou.dllo.findfood.util.MenuClickLisetener;
import com.lanou.dllo.findfood.util.Menuimage;
import com.lanou.dllo.findfood.volley.VolleySington;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/20.
 */
public class MenuFragment extends Fragment implements MenuClickLisetener,Menuimage {
    private RecyclerView recyclerView;
    private RequestQueue queue;
    private MenuReclyclerAdpter adpter;
    protected AlertDialog alertDialog;//定义一个对话框
    private TextView menudeals;
    private VolleySington volleySington;
    private MenuHealDataBaseUtli dataBaseUtli;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.activity_dariy, null);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
      requestData();

//        List<MenuResult> results = dataBaseUtli.queryAll();
//        if (results == null || results.size()==0){
//            requestData();
//        }
//        else {
//            adpter.addData(results);
//        }





    }

    public void initView() {
        recyclerView = (RecyclerView) getActivity().findViewById(R.id.DariyRecycleView);
        menudeals = (TextView) getActivity().findViewById(R.id.menudeals);
      //  dataBaseUtli = new MenuHealDataBaseUtli(getContext());

        queue = Volley.newRequestQueue(getActivity());
        adpter = new MenuReclyclerAdpter(getActivity());
        GridLayoutManager layout = new GridLayoutManager(getActivity(), 1);
        recyclerView.setLayoutManager(layout);
        recyclerView.setAdapter(adpter);
        adpter.setOnclickLisetener(this);
        adpter.setImageOnclick(this);


    }

    public void requestData() {
        final CookProgressDialog anmition = new CookProgressDialog(getContext(),R.id.animaImg);
        anmition.show();
        String url = "http://apis.baidu.com/tngou/cook/list";
        String httpArg = "id=0&page=1&rows=20";

        String path = url + "?" + httpArg;
        StringRequest request = new StringRequest(path, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                Menu menu = gson.fromJson(response, Menu.class);
                if (menu != null) {
                    List<MenuResult> results = menu.getTngou();
                    if (results != null && results.size() > 0) {
                        adpter.addData(results);
                      //  dataBaseUtli.deleAll();
                       // dataBaseUtli.saveData(results);


                    }
                }
                anmition.dismiss();

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            //在StringRespset结束之前写{}在大括号里面写 getHeaders() 自动重写方法
            //用Map方法键值对形势添加
            //添加请求头
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("apikey", "7df89e2ed7c7b92e1a2fe5ec8036d0cd");
                return map;
            }
        };
        queue.add(request);


    }

    @Override
    public void DetailsClick(MenuResult result) {

        menudeals.setText(result.getDescription());
        PopupWindow pw = new PopupWindow();
        View contentView = getActivity().getLayoutInflater().inflate(R.layout.popupwindow_show, null);
        pw.setContentView(contentView);
        pw.setWidth(500);
        pw.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        pw.setOutsideTouchable(true);
        pw.showAsDropDown(getView());


    }

    @Override
    public void MenuimageOnclick(MenuResult result) {
        Intent intent = new Intent(getActivity(), MenuDescipetinActivity.class);
        intent.putExtra("id", result.getId());
        startActivity(intent);
    }
}
